package com.xiangjiayi.study.controller;

import cn.hutool.core.date.DateTime;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DeepSeekController
{
//    以下是spring-ai创建ai模型的不同注入方式，都是为了在 Controller 中获取 AI 模型实例

    //V1：直接使用具体模型，spring-ai在pom文件里直接提供了deepseek模型的依赖，叫spring-ai-starter-model-deepseek，这里直接注入ai模型类
    @Resource
    private DeepSeekChatModel deepSeekChatModel;

    //V2：通过spring-ai提供的chatClient包装器使用模型，本质上还是依赖 V1 中的 DeepSeekChatModel 实例（通过构造方法传入），是对 V1 用法的「增强包装」。
    private ChatClient chatClient;
    public DeepSeekController(DeepSeekChatModel deepSeekChatModel)
    {
        this.chatClient = ChatClient.builder(deepSeekChatModel).build();//把v1的deepseekchatModel包装成chatClient
    }
    //V3：依赖spring-ai的顶层接口chatmodel，不关心具体哪个模型，也是spring-ai在pom文件里直接提供了chatmodel模型的依赖
    @Resource
    private ChatModel chatModel;

    //http://localhost:6001/chat
    @GetMapping("/chat")
    public String chat(@RequestParam(name = "question",defaultValue = "你是谁") String question)
    {
        String result = deepSeekChatModel.call(question); //spring-ai里面和ai模型对话的方式是.call()方法，括号里填userMessage
        System.out.println("chat result:" + result);
        return DateTime.now().toString()+"<br> \t"+result;
    }

    @RequestMapping("/chat2")
    public String chat2(@RequestParam(name = "question",defaultValue = "你是谁") String question)
    {
        ChatResponse chatResponse = chatClient.prompt(question).call().chatResponse(); //用封装了deepseek的Client调用对话
        String result = chatResponse.getResult().getOutput().getText();
        System.out.println("chat2 result:" + result);

        return DateTime.now().toString()+"<br> \t"+result;
    }

    @GetMapping("/chat3")
    public String chat3(@RequestParam(name = "question",defaultValue = "你是谁") String question)
    {
        String result = chatModel.call(question); //调用chatmodel的.call()功能进行对话
        System.out.println("chat3 result:" + result);
        return DateTime.now().toString()+"<br> \t"+result;
    }
}